/*
As3 Performances Analyzer
Copyright (C) 2011 Ankama Studio

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Contact : seyraud@ankama.com
Project : http://code.google.com/p/as3-performances-analyzer
*/

package types
{
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import interfaces.ITimelineData;
	
	public class TimeBar extends Sprite
	{
		private static var _colors : Dictionary = new Dictionary;
		public var data : ITimelineData;
		public var legend : String;
		public var opened : Boolean;
		private var _tf : TextField;
		private static const textFormat : TextFormat = new TextFormat;
		{
			textFormat.font = "verdana";
			textFormat.size = 10;
		}
		
		private static const matrix : Matrix= new Matrix();
		{
			matrix.createGradientBox(100, 100, 0, 0, 0);
			matrix.rotate(Math.PI / 2);
		}
		public function TimeBar(data : ITimelineData)
		{
			this.data = data;
			doubleClickEnabled = true;
			draw();
		}
		
		public function draw() : void
		{
			if(!_colors[data.name])
			{
				var i : uint;
				var r : uint;
				var g : uint;
				var b : uint;
				while(data.name && i++ < data.name.length)
				{
					switch(i % 3)
					{
						case 0:
							r += data.name.charCodeAt(i) % 256; break;
						case 1:
							g += data.name.charCodeAt(i) % 256; break;
						case 2:
							b += data.name.charCodeAt(i) % 256; break;
					}
				}
				r = 0xEE - r % 100;
				g = 0xEE - g % 100;
				b = 0xEE - b % 100;
				_colors[data.name] = ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF);
			}
			//graphics.beginGradientFill(GradientType.LINEAR, [c1, c2], [1,1],[0, 127], matrix);
			//graphics.lineStyle(1, c2);
			graphics.beginFill(_colors[data.name]);
			graphics.drawRect(0, 1, data.duration ? data.duration : 1, 30);
			graphics.endFill();
			
			if(width > 5)
			{
				_tf = new TextField();
				_tf.selectable = false;
				_tf.mouseEnabled = false;
				if(data.legend)
					_tf.text = data.legend + " (" + (data.duration) + " ms) ";
				else
					_tf.text = (data.duration) + " ms";
				_tf.setTextFormat(textFormat);
				_tf.width = width;
				_tf.height = height;
				addChild(_tf);
			}
			if(data.legend)
				legend = data.legend + " (" + (data.duration) + " ms)";
			else
				legend = (data.duration) + " ms";
		}
	}
}